﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

using CAION2.Views;

namespace CAION2
{
    public partial class App
    {
        private static Window2 _mw;
        
        private void ApplicationStartup(object sender, StartupEventArgs e)
        {
            try
            {
                _mw = new Window2();
                _mw.Show();
            }
            catch (Exception err)
            {
                File.WriteAllText(@"error.txt", err.StackTrace);
                Current.Shutdown();
            }
        }
    }

    public static class UiServices
    {
        private static bool _isBusy;
        
        public static void SetBusyState()
        {
            SetBusyState(true);
        }
        
        private static void SetBusyState(bool busy)
        {
            if (busy == _isBusy) return;

            _isBusy = busy;
            Mouse.OverrideCursor = busy ? Cursors.Wait : null;

            if (_isBusy)
            {
                new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, DispatcherTimerTick, Application.Current.Dispatcher);
            }
        }
        
        private static void DispatcherTimerTick(object sender, EventArgs e)
        {
            var dispatcherTimer = sender as DispatcherTimer;
            if (dispatcherTimer == null) return;

            SetBusyState(false);
            dispatcherTimer.Stop();
        }
    }
    
}
